package hystrix.ethan.springCloud.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import feign.ethan.springCloud.pojo.Dept;
import hystrix.ethan.springCloud.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ethan
 */
@RestController
@RequestMapping(value = "/dept")
@Slf4j
public class DeptController {
    @Resource
    private DeptService service;


    @GetMapping("detail/{id}")
    @HystrixCommand(fallbackMethod = "hystrixSelectOne")
    public Dept selectOne(@PathVariable("id") Integer id) {
        Dept dept = service.selectOne(id);
        if (ObjectUtils.isEmpty(dept)) {
            throw new RuntimeException("查询结果为空");
        }
        return dept;
    }

    public Dept hystrixSelectOne(@PathVariable("id") Integer id) {
        return new Dept().setDeptNo(0).setName("查询结果为空");
    }

}
